package com.yaoli.ajob;

import java.util.ArrayList;
import java.util.List;

/**
 * 顺时针打印矩阵
 *
 * Created by will on 2017/4/30.
 */
public class _054_SpiralMatrix {
    public static void main(String[] args) {
        int [][] matrix =
                {{ 1, 2, 3 },{ 4, 5, 6 }, { 7, 8, 9 }};
        _054_SpiralMatrix spiralMatrix = new _054_SpiralMatrix();
        spiralMatrix.spiralOrder(matrix);


        System.out.println(spiralMatrix.res);

    }

    List<Integer> res = new ArrayList<Integer>();

    public List<Integer> spiralOrder(int[][] matrix) {
        //b => begin
        int bCol = 0;
        int bRow = 0;

        //e => end
        int eCol = matrix[0].length - 1;
        int eRow = matrix.length - 1;

        while(bRow <= eRow && bCol <= eCol){
            calc(matrix,bCol,bRow,eCol,eRow);
            bCol++;
            bRow++;
            eCol--;
            eRow--;
        }

        return res;

    }

    public void calc(int [][] matrix,int bCol , int bRow,int eCol, int eRow){
        if(bCol == eCol){
            for(int i = bRow ;i <= eRow ; i++){
                res.add(matrix[i][bCol]);
            }
        }else if(bRow == eRow){
            for(int i = bCol ; i <= eCol; i++){
                res.add(matrix[bRow][i]);
            }
        }else{
            //不包含最后一个
            for(int i = bCol; i < eCol ; i++){
                res.add(matrix[bRow][i]);
            }
            for(int i = bRow; i < eRow ; i++){
                res.add(matrix[i][eCol]);
            }
            for(int i = eCol; i > bCol ; i--){
                res.add(matrix[eRow][i]);
            }
            for(int i = eRow; i > bRow ; i--){
                res.add(matrix[i][bCol]);
            }
        }
    }
}
